//引入vue
import Vue from "vue"
//实例化vue对象
const app = new Vue()

/**
 * 分享初始化
 * @param {Object} provider 服务提供者
 * @param {Object} title 分享标题
 * @param {Object} desc 分享描述
 * @param {Object} picture 分享图片
 * @param {Object} link 跳转链接
 */
function action(provider, title, desc, picture, link)
{
	//判断分享服务提供商
	switch (provider) {
		case 'weixin':			//微信分享
		case 'timeline':		//朋友圈分享
			uni.share({
				provider: "weixin",
				scene: provider == 'weixin' ? "WXSceneSession" : "WXSceneTimeline",
				type: 0,
				href: link,
				title: title,
				summary: desc,
				imageUrl: picture,
				success: function(res) {
					//调试信息
					console.log(provider + '分享成功');
				}
			});
		break;
		case 'system':
			//下载图片
			uni.downloadFile({
				url: picture,
				success: (res) => {
					//调用众益健康分享
					uni.shareWithSystem({
						type: typeof(picture) !== 'undefined' && picture.length > 0 ? 'image' : 'text',
						summary: title+' '+desc,
						href: link,
						imageUrl: res.tempFilePath,
						success: function(res) {
							//调试信息
							console.log(provider + '分享成功');
						}
					})
				},
				fail:() => {
					toastAlert('图片下载失败', 'none', 2000)
					reject('图片下载失败');
				}
			})
		break;
		default:
			uni.share({
				provider: provider,
				type: 0,
				href: link,
				title: title,
				summary: desc,
				imageUrl: picture,
				success: function(res) {
					//调试信息
					console.log(provider + '分享成功');
				}
			});
		break;
	}
	//返回成功
	return true;
}

//暴露方法
module.exports = {
	action: action
}